package com.yiren.manager.service.impl.messhall;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiren.core.service.GenericServiceImpl;
import com.yiren.core.utils.Favorites;
import com.yiren.entity.YrElderPayRecord;
import com.yiren.entity.YrElderWallet;
import com.yiren.entity.YrMealCard;
import com.yiren.entity.YrMealCardRechargeRecord;
import com.yiren.entity.YrMealsCostRecord;
import com.yiren.manager.repository.cost.YrElderPayRecordRepository;
import com.yiren.manager.repository.cost.YrMealCardRechargeRecordRepository;
import com.yiren.manager.repository.cost.YrMealsCostRecordRepository;
import com.yiren.manager.repository.elder.YrElderWalletRepository;
import com.yiren.manager.repository.messhall.YrMealCardRepositoty;
import com.yiren.manager.service.iface.messhall.YrMealCardService;

@Service
public class YrMealCardServiceImpl extends GenericServiceImpl<YrMealCard, String> implements YrMealCardService{

	@Autowired
	private YrElderWalletRepository elderWalletRepository;
	
	@Autowired
	private YrElderPayRecordRepository elderPayRecordRepository;
	
	@Autowired
	private YrMealCardRechargeRecordRepository mealCardRechargeRecordRepository;
	
	@Autowired
	private YrMealsCostRecordRepository mealsCostRecordRepository;
	
	@Autowired
	public YrMealCardServiceImpl(YrMealCardRepositoty repository) {
		super(repository);
	}

	/**
	 * 饭卡充值
	 */
	@Transactional
	public void executeRecharge(Favorites favorites) {
		YrMealCard mealCard = favorites.getFavorite(YrMealCard.class);
		
		YrElderWallet elderWallet =  favorites.getFavorite(YrElderWallet.class);
		YrElderPayRecord elderPayRecord = favorites.getFavorite(YrElderPayRecord.class);
		List<YrMealCardRechargeRecord> mealCardRechargeRecords = favorites.getFavorite(List.class);
				
		this.repository.save(mealCard);
		if(null!=elderWallet){
			elderWalletRepository.save(elderWallet);
		}
		
		if(null!=elderPayRecord){
			elderPayRecordRepository.save(elderPayRecord);
		}
		for(YrMealCardRechargeRecord mealCardRechargeRecord : mealCardRechargeRecords){
			if(null!=mealCardRechargeRecord){
				mealCardRechargeRecordRepository.save(mealCardRechargeRecord);
			}
		}
	}

	@Transactional
	public void executeSlotMeal(Favorites favorites) {
		YrMealCard mealCard = favorites.getFavorite(YrMealCard.class);
		ArrayList<YrMealsCostRecord> mealsCostRecords = favorites.getFavorite(ArrayList.class);
		
		this.repository.save(mealCard);
		for(YrMealsCostRecord mealsCostRecord : mealsCostRecords){
			mealsCostRecordRepository.save(mealsCostRecord);
		}
	}
}
